package com.feige.common.handler;

import com.feige.common.ex.ApiException;
import com.feige.common.result.ErrorHandler;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.WebRequest;

import java.util.List;

/**
 * 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ErrorHandler handleValidException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getDefaultMessage();
            }

            if (fieldError == null) {
                List<ObjectError> allErrors = bindingResult.getAllErrors();
                if (allErrors != null && allErrors.size() > 0) {
                    ObjectError objectError = allErrors.get(0);//只拿一个错误信息即可
                    message = objectError.getDefaultMessage();
                }
            }
        }
        return ErrorHandler.failed(message);
    }

    @ExceptionHandler(value = BindException.class)
    public ErrorHandler handleValidException(BindException e) {
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getDefaultMessage();
            }
        }
        return ErrorHandler.failed(message);
    }

    @ExceptionHandler(value = NullPointerException.class)
    public ErrorHandler NullPointerException(NullPointerException e) {
        return ErrorHandler.failed("空指针异常");

    }

    @ExceptionHandler({ApiException.class})
    public ErrorHandler handler(ApiException e) {
        return ErrorHandler.failed(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    public ErrorHandler exception(IllegalArgumentException e) {
        return ErrorHandler.failed(e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public ErrorHandler exception(Exception ex, WebRequest request) {
        return ErrorHandler.failed(StringUtils.defaultIfEmpty(ex.getMessage(), "系统异常"));
    }


    private String getRequestUrl() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String uri = request.getRequestURI();
        String method = request.getMethod();
        return method + ":" + uri;
    }
}
